<?php
namespace app\controller;

use app\BaseController;
use app\model\User;
use app\common\Token;


class UserCenter extends BaseController
{
    public function login()
    {
        $param = $this->request->param();
        $account=$param['account'];
        $password=$param['password'];
        $db = new User();
        $isLogin = $db->login($account, $password);
        if ($isLogin) {
            $json=array('data'=>['token'=>Token::encode(['id'=>1])],'code'=>0,'tips'=>'ok');
        } else {
            $json=array('data'=>['token'=>""],'code'=>1001,'tips'=>'账号或密码错误');
        }
        return json($json, 200);
    }
    public function register()
    {
        $param = $this->request->param();
        $account=$param['account'];
        $password=$param['password'];
        if (!($account&&$password)) {
            $json=array('data'=>[],'code'=>1001,'tips'=>'账号密码不能为空');
            return json($json, 200);
        }
        $db = new User();
        if ($db->findPhone($account)) {
            $json=array('data'=>[],'code'=>1002,'tips'=>'该账号已创建');
            return json($json, 200);
        }
        $data=[
            'nickName'=>'管理员',
            'account'=>$account,
            'passwd'=>$password,
            'createTime'=>time(),
            'updateTime'=>time(),
        ];
        $is = $db->register($data);
        if ($is) {
            $json=array('data'=>[],'code'=>0,'tips'=>'ok');
        } else {
            $json=array('data'=>[],'code'=>1001,'tips'=>'创建用户失败');
        }
        return json($json, 200);
    }
}
